import { loginByUsername, loginGetUserInfo,loginWithSmsCode } from "@/api/user";
import {
  getToken,
  setToken,
  removeToken,
  setUserInfo,
  removeUserInfo,
  setDepInfo
} from "@/utils/auth.js";
// import { resetRouter } from "@/router";
import userIcon from "@/icons/svg/user.svg";
const getDefaultState = () => {
  return {
    userIcon: userIcon,
    token: getToken(),
    name: "",
    avatar: "",
    userInfo: null,
    loadding:'no'
  };
};

const state = getDefaultState();

const mutations = {
  showloadding :(state,load) =>{
    state.loadding = load
  },
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo;
  }
};

const actions = {
  setloadding({ commit }, load){
    commit('showloadding',load)
  },
  // user login
  login({ commit }, userInfo) {
    const { name, password } = userInfo;
    return new Promise((resolve, reject) => {
      loginByUsername({ name: name.trim(), password: password })
        .then(response => {
          
          const { signature, data } = response;
          commit("SET_TOKEN", data.token);
          console.log(1111111)
          console.log(data.token)
         
          
          commit("SET_NAME", data.name);
          commit('SET_AVATAR', userIcon)
          let userInfo = JSON.parse(JSON.stringify(data))
          delete userInfo.menuVoList
          setUserInfo(JSON.stringify(userInfo))
          debugger
          JSON.stringify(data.menuVoList)
          // setToken(JSON.stringify(data.token));
          sessionStorage.setItem('token',data.token)
          sessionStorage.setItem("buttenpremissions",JSON.stringify(data.menuVoList))
          sessionStorage.setItem(
            "authority",
            JSON.stringify([
              "analysis",
              "user_statistics",
              "device_statistics",
              "device_list",
              "device_detail",
              "report_list",
              "report_detail",
              "record_list",
              "merchant_info",
              "merchant_list",
              "merchant_detail",
              "merchant_create",
              "consumer_list",
              "consumer_detail",
              "account_list",
              "role_list",
              "role_detail",
              "role_create"
            ])
          );
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  getUserInfo({commit},userInfo) {
    let token = JSON.parse(JSON.stringify(userInfo))
    return new Promise((resolve, reject) => {
      loginGetUserInfo(token)
        .then(response => {
          
          const { signature, data } = response;
          data.token = token
          debugger
          commit("SET_TOKEN", data.token);
          console.log(1111111)
          console.log(data.token)
         
          
          commit("SET_NAME", data.name);
          commit('SET_AVATAR', userIcon)
          let userInfoData = JSON.parse(JSON.stringify(data))
          delete userInfoData.menuVoList
          setUserInfo(JSON.stringify(userInfoData))
          
          JSON.stringify(data.menuVoList)
          // setToken(JSON.stringify(data.token));
          sessionStorage.setItem('token',data.token)
          sessionStorage.setItem("buttenpremissions",JSON.stringify(data.menuVoList))
          sessionStorage.setItem(
            "authority",
            JSON.stringify([
              "analysis",
              "user_statistics",
              "device_statistics",
              "device_list",
              "device_detail",
              "report_list",
              "report_detail",
              "record_list",
              "merchant_info",
              "merchant_list",
              "merchant_detail",
              "merchant_create",
              "consumer_list",
              "consumer_detail",
              "account_list",
              "role_list",
              "role_detail",
              "role_create"
            ])
          );
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },
  loginPhone({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      loginWithSmsCode(userInfo)
        .then(response => {
          const { signature, data } = response;
          commit("SET_TOKEN", data.token);
          // setToken(data.token);
          
          commit("SET_NAME", data.name);
          commit('SET_AVATAR', userIcon)
          let userInfo = JSON.parse(JSON.stringify(data))
          delete userInfo.menuVoList
          setUserInfo(JSON.stringify(userInfo))
          JSON.stringify(data.menuVoList)
          sessionStorage.setItem('token',data.token)
          sessionStorage.setItem("buttenpremissions",JSON.stringify(data.menuVoList))
          sessionStorage.setItem(
            "authority",
            JSON.stringify([
              "analysis",
              "user_statistics",
              "device_statistics",
              "device_list",
              "device_detail",
              "report_list",
              "report_detail",
              "record_list",
              "merchant_info",
              "merchant_list",
              "merchant_detail",
              "merchant_create",
              "consumer_list",
              "consumer_detail",
              "account_list",
              "role_list",
              "role_detail",
              "role_create"
            ])
          );
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  getUerInfo({ commit }, code) {
    return new Promise((resolve, reject) => {
      getUserInfo(code)
        .then(async response => {
          const { signature, data } = response;
          commit("SET_TOKEN", data.token);
          // setToken(data.token);
          sessionStorage.setItem('token',data.token)
          commit("SET_NAME", data.userName);
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken(); // must remove  token  first
      // resetRouter();
      sessionStorage.clear()
      removeUserInfo();
      commit("RESET_STATE");
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
